<script>
import outMain from "@/components/outMain.vue";
import elSelect from "@/components/public/elSelect.vue";
import edit from "@/components/public/edit.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { ArtAdd, ArtEdit, ArtShow } from "@/http/api";
import Quill from "quill";
import "quill/dist/quill.snow.css"; // 引入样式文件
export default {
  components: { outMain, elSelect, edit },
  name: "",
  data() {
    return {
      quill: null,
      queryParams: {
        article_title: "",
        article_sort: "1",
        article_content: "",
      },
      rules: {
        article_title: [
          {
            message: "标题不能为空",
            required: true,
            trigger: "blur",
          },
        ],
        article_content: [
          {
            message: "内容不能为空",
            required: true,
            trigger: "blur",
          },
        ],
      },
      maskTitle: "",
      queryData: {
        page: 1,
        per_page: 10,
      },
      total: 0,
    };
  },
  props: {},
  setup() {},
  methods: {
    async saveFn() {
      this.queryParams.article_content =
        this.quill.container.firstChild.innerHTML;
      const valid = await this.$refs.ruleForm.validate();
      if (!valid) {
        return false;
      }
      const { data } =
        this.maskTitle == "新增"
          ? await ArtAdd(this.queryParams)
          : await ArtEdit(this.queryParams);
      if (data.code == 1) {
        ElMessage({
          type: "success",
          message: this.maskTitle + "成功",
        });
        setTimeout(() => {
          this.$router.back();
        }, 1000);
      }
    },
    editFn(row) {
      this.queryParams = {
        article_sort: row.article_sort,
        article_title: row.article_title,
        article_content: row.article_content,
        article_id: row.article_id,
      };
      this.quill.root.innerHTML = row.article_content;
      this.maskTitle = "编辑";
    },
    addFn() {
      this.queryParams = {
        article_sort: "1",
        article_title: "",
        article_content: "",
      };
      this.maskTitle = "新增";
    },
    getData() {
      ArtShow({ article_id: this.$route.query.id }).then((res) => {
        if (res.data.code == 1) {
          console.log(res.data.data);
          this.editFn(res.data.data);
        }
      });
    },
  },
  mounted() {
    setTimeout(() => {
      this.quill = new Quill(this.$refs.quillEditorRef, {
        theme: "snow",
        modules: {
          toolbar: [
            // 默认的
            [{ header: [1, 2, 3, false] }],
            ["bold", "italic", "underline", "link"],
            [{ list: "ordered" }, { list: "bullet" }],
            ["clean"],
            // 新增的
            ["image"],
          ],
        },
        placeholder: "平等表达，友善交流",
      });
      setTimeout(() => {
        if (this.$route.query.id) {
          this.getData();
        } else {
          this.addFn();
        }
      }, 1);
    }, 10);
  },
  created() {},
};
</script>
<template>
  <outMain>
    <div class="formCont">
      <div class="formStep">
        <div class="formHead">
          <div>帮助中心</div>
        </div>
        <div class="formMain">
          <div class="stepCont">
            <div class="formDes">
              <div class="desTitle">
                <div>{{ maskTitle }}帮助中心</div>
              </div>
              <el-form ref="ruleForm" :rules="rules" :model="queryParams">
                <el-form-item
                  label-width="120"
                  label="帮助标题"
                  prop="article_title"
                >
                  <el-input
                    v-model="queryParams.article_title"
                    placeholder="帮助标题"
                  />
                </el-form-item>
                <el-form-item
                  label-width="120"
                  label="优先级"
                  prop="article_sort"
                >
                  <el-input
                    v-model="queryParams.article_sort"
                    placeholder="帮助优先级"
                  />
                </el-form-item>
                <el-form-item
                  label-width="120"
                  label="帮助内容介绍"
                  prop="article_content"
                >
                  <div ref="quillEditorRef" class="quill-editor"></div>
                </el-form-item>
                <el-form-item label-width="120">
                  <btn value="保存设置" @ok="saveFn"></btn>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less">
.quill-editor {
  height: 250px;
  width: 100%;
}
</style>
<style>
.ql-snow {
  width: 100%;
}
</style>
